<template>
  <div ref="chart"  :style="chartStyle">
  </div>
</template>

<script>
import echarts from 'echarts';

export default {
  name: 'barcircle',
  props: {
    config: {
      required: true,
    },
    chartStyle: {
      require: false,
      type: Object,
      default: () => ({
        height: '100%',
        position: 'absolute',
        width: '100%',
        top: '0px',
        bottom: '0px',
        left: '0px',
      }),
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  created() {

  },
  mounted() {
    this.renderChart();
  },
  methods: {
    handleResize() {
      if (this.chart) {
        this.chart.resize();
      }
    },
    random(min, max) {
      return parseInt(Math.random() * (max - min) + min, 10);
    },
    renderChart() {
      if (!this.chart) {
        this.chart = echarts.init(this.$refs.chart, this.config.theme);
      }
      const option = this.makeOptions(this.config, this.chartData);
      this.chart.clear();
      this.chart.setOption(option);
      setTimeout(() => {
        this.chart.resize();
      }, 100);
    },
    makeOptions() {
      const xData = ['a', 'b', 'c', 'd', 'e', 'f'];
      const yData = [];
      const barData = [];

      for (let i = 0; i < xData.length; i += 1) {
        const value = this.random(5, 100);
        yData.push(value);
        barData.push(parseInt(((value / 3) * 2) * (this.random(1, 100) / 100), 10));
      }

      return {

        backgroundColor: '#062544',
        grid: {
          top: '10%',
          left: '-5%',
          bottom: '5%',
          right: '5%',
          containLabel: true,
        },
        tooltip: {
          trigger: 'item',
        },
        animation: false,
        xAxis: [
          {
            type: 'category',
            data: xData,
            axisTick: {
              alignWithLabel: true,
            },
            axisLine: {
              show: false,
            },
            axisLabel: {
              textStyle: {
                color: '#ddd',
              },
              margin: 30,
            },
            interval: 1,
          },
        ],
        yAxis: [
          {
            show: false,
            type: 'value',
          },
        ],
        series: [
          {
            name: '上部圆',
            type: 'pictorialBar',
            silent: true,
            symbolSize: [40, 10],
            symbolOffset: [0, -6],
            symbolPosition: 'end',
            z: 12,
            label: {
              normal: {
                show: true,
                position: 'top',
                fontSize: 15,
                fontWeight: 'bold',
                color: '#5BFCF4',
              },
            },
            color: '#5BFCF4',
            data: yData,
          },
          {
            name: '底部圆',
            type: 'pictorialBar',
            silent: true,
            symbolSize: [40, 10],
            symbolOffset: [0, 7],
            z: 12,
            color: '#5BFCF4',
            data: yData,
          },

          {
            name: '设备数量',
            type: 'bar',
            barWidth: '40',
            barGap: '10%', // Make series be overlap
            barCateGoryGap: '10%',
            itemStyle: {
              normal: {
                color: new echarts.graphic.LinearGradient(0, 0, 0, 0.7, [
                  {
                    offset: 0,
                    color: 'rgba(210,210,210,0.3)',
                  },
                  {
                    offset: 1,
                    color: '#5BFCF4',
                  },
                ]),
                opacity: 0.8,
              },
            },
            data: yData,
          },
          {
            name: '告警数量',
            type: 'bar',
            barWidth: 40,
            z: 12,
            barGap: '-100%',
            itemStyle: {
              // lenged文本
              opacity: 0.7,
              color() {
                return new echarts.graphic.LinearGradient(
                  0,
                  0,
                  0,
                  1,
                  [
                    {
                      offset: 0,
                      color: '#EB3B5A', // 0% 处的颜色
                    },
                    {
                      offset: 1,
                      color: '#FE9C5A', // 100% 处的颜色
                    },
                  ],
                  false,
                );
              },
            },

            data: barData,
          },
          {
            name: '上部圆',
            type: 'pictorialBar',
            silent: true,
            symbolSize: [40, 10],
            symbolOffset: [0, -6],
            symbolPosition: 'end',
            z: 12,
            color: '#EB3B5A',
            data: barData,
          },
          {
            name: '底部圆',
            type: 'pictorialBar',
            silent: true,
            symbolSize: [40, 10],
            symbolOffset: [0, 7],
            z: 12,
            color: '#FE9C5A',
            data: barData,
          },
        ],
      };
    },
  },
  watch: {
    config: {
      deep: true,
      handler() {
        if (this.config.needResize) {
          this.config.needResize = false;
          this.handleResize();
        }
      },
    },
  },
};
</script>

<style scoped lang="stylus">

</style>
